package com.ydj.framework.mysql;


import com.baomidou.mybatisplus.annotation.*;
import com.google.gson.Gson;
import com.ydj.framework.utils.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
@Setter
@Accessors(chain = true)
@MappedSuperclass
public class BaseEntity implements Serializable {
    @Transient
    protected static Gson gson = new Gson();

    /** 用了雪花生成ID */
    @Id
    @Column(columnDefinition = "bigint NOT NULL AUTO_INCREMENT COMMENT 'id'")
    @TableId(type = IdType.ASSIGN_ID)
    protected Long id;

    @Column(columnDefinition = "datetime comment '创建时间'", nullable = false, updatable = false)
    @TableField(fill = FieldFill.INSERT)
    protected Date createTime;

    @Column(columnDefinition = "datetime comment '修改时间'", updatable = false)
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Date updateTime;

    protected List<String> jsonToStringList(String json) {
        if (StringUtils.isBlank(json)) {
            return new ArrayList<>();
        }
        return JsonUtils.jsonToStringList(json);
    }

    protected List<Long> jsonToLongList(String json) {
        if (StringUtils.isBlank(json)) {
            return new ArrayList<>();
        }
        return JsonUtils.jsonToLongList(json);
    }

    protected String stringListToJson(List<String> list) {
        if (list == null) {
            return "[]";
        } else {
            return JsonUtils.listToJson(list);
        }
    }

    protected String longListToJson(List<Long> list) {
        if (list == null) {
            return "[]";
        } else {
            return JsonUtils.listToJson(list);
        }
    }
}